<?php
class UserLikesController extends AppController{
	var $name = 'UserLikes';
	var $uses = array('UserLike','UserImageBoard');
	var $helpers = array('Html','Session');
	var $components = array('Session');

	public function likeImage($userImageBoardId = null){
		if($userImageBoardId){
			
			$userImageBoard = $this->UserImageBoard->findById($userImageBoardId);
			
			$userImageBoard['UserImageBoard']['count_like'] += 1;
			$this->UserImageBoard->save($userImageBoard);

			$like['UserLike'] = array(
    							'user_id'=>$this->Auth->user('id'),
    							'user_image_board_id'=>$userImageBoardId,
    							'create'=>date('m/d/Y h:i:s a', time()));
			$this->UserLike->create();
			if( $this->UserLike->save($like) ){
    			
    			echo "<script language=javascript>window.location.replace(document.referrer);</script>";
    			//return $this->redirect( array('controller'=>'UserImageBoards','action'=>'listImages'));
    		}
		}
		$this->Session->setFlash(__('error like'));
	}

	public function unlikeImage($userImageBoardId = null){
		if($userImageBoardId){

			$userImageBoard = $this->UserImageBoard->findById($userImageBoardId);
			$userImageBoard['UserImageBoard']['count_like'] -= 1;
			$this->UserImageBoard->save($userImageBoard);

			$like = $this->UserLike->find('all',array(
													'conditions'=>array(
														'UserLike.user_image_board_id'=>$userImageBoardId,
														'UserLike.user_id'=>$this->Auth->user('id'))
													));
			if($like){
				if($this->UserLike->delete($like[0]['UserLike']['id'])){
					
					echo "<script language=javascript>window.location.replace(document.referrer);</script>";
					//return $this->redirect( array('controller'=>'UserImageBoards','action'=>'listImages'));
				}
			}
		}
		$this->Session->setFlash(__('error unlike'));
	}
}
?>